// pages/account/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),
formatTime = require("../../../component/common.js")
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
import { hexMD5 } from '../../../utils/md5.js';
const CryptoJS = require("../../../utils/aesUtil.js")
const format = rate => Math.min(Math.max(rate, 0), 100);
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phoneNum:'去绑定',
    is_has_pay_pwd:0,
    payPwd:'',
    code:'',
    sendCodeState:0,
    time: 60 * 1000,
    secondsMill:0,
    timeData: {},
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onHttpInit();
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/wxUser/getAccountInfo', {}, function (res) {
      console.log("账号绑定 数据", res)
      if(res.data.code == 0){
        that.data.phoneNum = res.data.data.phone_num;
        that.setData({
          is_has_pay_pwd: res.data.data.is_has_pay_pwd,
          phoneNum: res.data.data.phone_num!=undefined &&  res.data.data.phone_num!='' ? res.data.data.phone_num : '去绑定',
        })
      }
      that.setData({
        netWork: true,
      })
    }, function () {
        that.setData({ netWork: false, hasData: false })
    })
  },
  onShow(options){
  },
  startTime() {
    const countDown = this.selectComponent('.control-count-down');
    countDown.start();
    console.log('startTime')
  },
  finishedTime(){
    this.setData({
      sendCodeState: 0,
    })
  },
  onChange(e) {
    this.setData({
      time: e.detail 
    });
  },
  phoneNumInput(e){
    this.data.phoneNum = e.detail;
  },
  payPwdInput(e){
    
    // 统一使用MD5加密
    var ps = hexMD5(e.detail);
    // this.data.payPwd = CryptoJS.Encrypt(e.detail);
    this.data.payPwd = ps;
  },
  codeInput(e){
    this.data.code = e.detail;
  },
  test(){
    this.setData({
      sendCodeState:1
    })
    // this.startTime()
  },
  onHttpSendCode(e){
    if(this.data.phoneNum == ''){
      Toast.fail("请输入手机号");
      return;
    }
    var that = this;
    fetchApi.httpPost('/wxUser/sendCode', {
      phone_num:that.data.phoneNum,
      code_type:0,
    }, function (res) {
      console.log("发送验证码", res)
      if(res.data.code == 0){
        Toast.success(res.data.msg)
        that.setData({
          sendCodeState:1
        })
      }else{
        Toast.fail(res.data.msg)
      }
    }, function () {
        that.setData({ netWork: false, hasData: false })
    })
  },
  goSubmit(){
    if(this.data.payPwd == ''){
      Toast.fail("请输入支付密码");
      return ;
    }
    if(this.data.code == ''){
      Toast.fail("请输入验证码");
      return ;
    }

    this.onHttpUpdatePwd();
  },
  onHttpUpdatePwd(){
    var that = this;
    fetchApi.httpPost('/wxUser/updatePayPwd', {
      phone_num:that.data.phoneNum,
      pwd: that.data.payPwd,
      code:that.data.code,
    }, function (res) {
      console.log("修改密码", res)
      if(res.data.code == 0){
        Toast.success(res.data.msg)
        setTimeout(function(){
          wx.navigateBack({
            delta: -1,
          })
        },1000)
      }else{
        Toast.fail(res.data.msg)
      }
    }, function () {
        that.setData({ netWork: false, hasData: false })
    })
  },
})